(use-modules
 (ice-9 exceptions))

;; =========
;; UTILITIES
;; =========

(define even-simpler-display
  (lambda (sth)
    (display
     (simple-format
      #f "~a\n" sth))))

;; =====
;; INTRO
;; =====

;; Guile compound exceptions are similar to rnrs exceptions (conditions) in
;; concept, but have different names for procedures and simple (non-compound)
;; exceptions. The way one accesses the simple exceptions inside a compound
;; exception also differs.

;; ===================
;; COMPOUND EXCEPTIONS
;; ===================

(define divide
  (lambda (dividend divisor)
    (cond
     [(= divisor 0)
      (raise-exception
       (make-exception
        (make-non-continuable-error)
        (make-exception-with-message "division by zero")
        (make-exception-with-irritants (list dividend divisor))
        (make-exception-with-origin 'divide)))]
     [else
      (/ dividend divisor)])))


(with-exception-handler
    (lambda (exception)
      (display
       (simple-format
        #f "warning?: ~a\n" (warning? exception)))
      (display
       (simple-format
        #f "exception?: ~a\n" (exception? exception)))
      (display
       (simple-format
        #f "exception-with-message?: ~a\n"
        (exception-with-message? exception)))
      (display
       (simple-format
        #f "exception-message: ~a\n"
        (exception-message exception)))
      (display
       (simple-format
        #f "exception: ~a\n" exception)))
  (lambda ()
    (divide 2 0))
  #:unwind? #t)
